<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta http-equiv="refresh" content="60">
    <title>我在这儿</title>
    <script>
        if (!location.search) {
            location.href = 'https://ditu.amap.com/';
        };
    </script>
    <script src="https://webapi.amap.com/maps?v=1.4.14&key=f9084e04ba96782ae5d791aee1b4332e"></script>
    <style>
        html,
        body,
        #container {
            margin: 0;
            height: 100%;
        }

        .in {
            position: absolute;
            top: 0;
            right: 0;
            z-index: 200;
        }

        #shijian {
            color: #00f;
        }
    </style>
</head>

<body>
    <div id="container"></div>
    <div class="in">
        <input type="button" value="@到达我这里要多久？" id="shijian"><br><br>
    </div>
    <script>
        let map = new AMap.Map("container");
        let gps;
        let xhr = new XMLHttpRequest();
        xhr.open('GET', 'https://tsapi.amap.com/v1/track/terminal/lastpoint' + location.search + '&correction=n', true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                let data = JSON.parse(xhr.responseText).data;
                gps = data.location.split(',');
                let gps2 = [gps];
                let timems = data.locatetime;
                let nowtimems = new Date().getTime();
                timems = parseInt((nowtimems - timems) / 1000);
                AMap.convertFrom(gps2, "gps", function (status, result) {
                    if (result.info === "ok") {
                        gps2 = result.locations;//gps转高德坐标
                        let marker = new AMap.Marker({
                            map: map,
                            position: gps2[0]//添加点
                        });
                        map.setFitView();//聚焦
                        function text() {//添加文字
                            marker.setLabel({
                                offset: new AMap.Pixel(0, -25),
                                content: timems / 60 >= 1 ? parseInt(timems / 60) + '分钟前位置' : timems + '秒前位置',
                                direction: 'right'
                            });
                            timems++;
                        }
                        text();
                        if (timems < 60) {
                            let runid = setInterval(function () {
                                text();
                                if (timems > 60) {
                                    clearInterval(runid)
                                };
                            }, 1000
                            )
                        }
                    }
                });
            }
        }
        xhr.send(null);
        shijian.onclick = function () {
            this.value = '请稍等...';
            // 以下定位模块
            AMap.plugin('AMap.Geolocation', function () {
                var geolocation = new AMap.Geolocation({
                    enableHighAccuracy: true,//是否使用高精度定位，默认:true
                    timeout: 15000,          //超时
                });
                geolocation.getCurrentPosition(function (status, result) {
                    if (status == 'complete') {
                        let my = [result.position.lng, result.position.lat];
                        let address = result.formattedAddress.replace('市', '市\n');
                        // 以下路线规划
                        AMap.plugin('AMap.Driving', function () {
                            let driving = new AMap.Driving({
                                policy: AMap.DrivingPolicy.LEAST_TIME//最快方式
                            })
                            driving.search(gps, my, function (status, result) {
                                let fen = parseInt(result.routes[0].time / 60)
                                fen = fen / 60 >= 1 ? parseInt(fen / 60) + '小时' + fen % 60 + '分钟' : fen + '分钟'
                                shijian.value = '@到达我这里要多久？';
                                alert('您的位置大概是：\n' + address + '\n\n到达您这里大概需要时间：\n' + fen)
                            })
                        })
                        // 以上路线规划
                    }
                });
            });
            // 以上定位模块
        }
    </script>
</body>

</html>
